package parser;

import parser.prestige.AbstractNode;
import parser.prestige.IXmlElement;
import parser.prestige.meldunek.xml.Meldunek;

import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: marcin.kaluza
 * Date: 29.11.13
 * Time: 09:49
 * Klasa bazowa dla parserów.
 */
public abstract class Parser {
    protected String outputFileName = null;
    protected String inputFileName = null;
    private BufferedReader br = null;
    private FileReader fr = null;

    public List<AbstractNode> readInput(String inputFileName, String outputFileName) {
        this.outputFileName = outputFileName;
        return readInput(inputFileName);
    }

    public List<AbstractNode> readInput(String inputFileName) {
        try {
            this.inputFileName = inputFileName;
            List<String> lines = new ArrayList<String>();
            fr = new FileReader(inputFileName);
            br = new BufferedReader(fr);
            String line = br.readLine();
            while (line != null) {
//                line = line.trim();
                lines.add(line);
                line = br.readLine();
            }
            //sparsuj i zapisz
//            writeOutput(parseInput(lines, inputFileName));
            return parseInput(lines, inputFileName);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
//            System.out.println("File "+inputFileName+" not found.");
            System.exit(0);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                br.close();
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * Parsuje dane po wczytaniu
     */
    public abstract List<AbstractNode> parseInput(List<String> lines, String inputFileName);

    /**
     * Zapisuje
     * http://www.vogella.com/articles/JavaXML/article.html#javastax_write
     */
    public abstract void writeOutput(List<AbstractNode> nodes);
}
